//
//  RecommendViewController.m
//  Qyer
//
//  Created by dlios on 15/10/16.
//  Copyright (c) 2015年 GF. All rights reserved.
//

#import "RecommendViewController.h"




@interface RecommendViewController ()<ScrollImageViewDelegate,UITableViewDelegate, UITableViewDataSource>
{
    NSInteger number;
}
@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    ScrollImageView *scIamgeView = [[ScrollImageView alloc] initWithFrame:CGRectMake(5 * WIDTH, 0 * HEIGHT, SCREEN_WIDTH, 430 * HEIGHT)];
    [scIamgeView setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:scIamgeView];
    [scIamgeView.scView setUserInteractionEnabled:YES];
    [scIamgeView setDelegate:self];

    _rmArray = [NSMutableArray array];
    [self.view setBackgroundColor:[UIColor colorWithRed:240 / 255. green:240 / 255. blue:240 / 255. alpha:1.]];
    number = 0;
    [scIamgeView setBackgroundColor:[UIColor colorWithRed:240 / 255. green:240 / 255. blue:240 / 255. alpha:1.]];
    [self.view addSubview:scIamgeView];
    [scIamgeView.scView setUserInteractionEnabled:YES];
    [scIamgeView setDelegate:self];
    [scIamgeView setDelegate:self];
    [scIamgeView setDelegate:self];
    [scIamgeView setDelegate:self];
    _rmTable = [[UITableView alloc] initWithFrame:CGRectMake(5 * WIDTH, 5 * HEIGHT, 365 * WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    [_rmTable setDelegate:self];
    [_rmTable setDataSource:self];
    [_rmTable setShowsVerticalScrollIndicator:NO];
    [_rmTable setTableHeaderView:scIamgeView];
    [self.view addSubview:_rmTable];
    [self rmTableAfn];
    [_rmTable addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(addHeader)];
    [_rmTable addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(addFooter)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(jnButtonClick:)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchButtonClick:)];
    
}

- (void)searchButtonClick: (UIButton *)sender{
    GFSearchViewController *search = [[GFSearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}

- (void)pushString:(NSString *)string{
    WebViewController *web = [[WebViewController alloc] init];
    web.webString = string;
    [self.navigationController pushViewController:web animated:YES];
}

#pragma mark-锦囊点击代理方法
- (void)jnButtonClick: (UIButton *)sender{
    GFJnViewController *jnView = [[GFJnViewController alloc] init];
    [self.navigationController pushViewController:jnView animated:YES];
}
- (void)addHeader{
    [_rmArray removeAllObjects];
    [self rmTableAfn];
}

- (void)addFooter{
    if (number < 1) {
        number = 1;
    }
    number++;
    [self rmTableAfn];
}
- (void)rmTableAfn{
    [AFN getdataWithString:[NSString stringWithFormat:@"http://open.qyer.com/qyer/recommands/trip?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=868842022014563&track_app_version=6.7.1&track_app_channel=xiaomi&track_device_info=hermes&track_os=Android5.0.2&app_installtime=1444898310285&lat=38.883524&lon=121.544823&type=index&page=%ld&count=10", number] block:^(id block) {
        
        for (NSDictionary *dic in [block objectForKey:@"data"]) {
            RecommendModel *model = [RecommendModel modelWithDictionary:dic];
            [_rmArray addObject:model];
        }
        [_rmTable reloadData];
        [_rmTable.header endRefreshing];
        [_rmTable.footer endRefreshing];
    } BLOCK:^(id BLOCK) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *str = @"看热门游记";
    return str;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _rmArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90 * HEIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"cell";
    GFRmTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[GFRmTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    RecommendModel *model = [_rmArray objectAtIndex:indexPath.row];
    [cell getRmModel:model];
    return cell;
}

#pragma mark-封装(big  left right)按钮代理回调方法实现
- (void)setString:(NSString *)string{
    WebViewController *webView = [[WebViewController alloc] init];
    webView.webString = string;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)setAString:(NSString *)string{
    WebViewController *webView = [[WebViewController alloc] init];
    webView.webString = string;
    [self.navigationController pushViewController:webView animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RecommendModel *model = [_rmArray objectAtIndex:indexPath.row];
    NSString *preString = @"http://appview.qyer.com/bbs/thread-";
NSString *hasString = @"-1.html?source=app&client_id=qyer_android&track_user_id=&track_deviceid=868842022014563&track_app_version=6.7.1";
    NSString *comString = [NSString stringWithFormat:@"%@%@%@",preString, model.cId, hasString];
    WebViewController *webView = [[WebViewController alloc] init];
    webView.webString = comString;
    [self.navigationController pushViewController:webView animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
